<template>
  <div class="login">
    <div class="login_left">
      <img src="@/assets/images/login_bg.png" />
    </div>
    <div class="login_right">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        label-position="top"
        class="login_form"
      >
        <h2 class="title">欢迎使用舒城县旅游管理平台</h2>
        <el-form-item prop="username" label="用户名">
          <el-input
            v-model="loginForm.username"
            type="text"
            maxlength="16"
            auto-complete="off"
            @focus="isUsername = true"
            @blur="isUsername = false"
            placeholder="请输入用户名"
          >
            <svg-icon
              slot="prefix"
              icon-class="user"
              v-bind:class="{ 'color-class': isUsername }"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input
            v-model="loginForm.password"
            type="password"
            maxlength="20"
            auto-complete="off"
            placeholder="请输入密码"
            @focus="isPassword = true"
            @blur="isPassword = false"
            @keyup.enter.native="handleLogin"
            show-password
          >
            <svg-icon
              slot="prefix"
              icon-class="password"
              v-bind:class="{ 'color-class': isPassword }"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaOnOff" label="验证码">
          <el-row :gutter="4">
            <el-col :span="14"
              ><el-input
                v-model="loginForm.code"
                auto-complete="off"
                maxlength="4"
                placeholder="请输入验证码"
                @focus="isValidCode = true"
                @blur="isValidCode = false"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon
                  slot="prefix"
                  icon-class="validCode"
                  v-bind:class="{ 'color-class': isValidCode }"
                  class="el-input__icon input-icon"
                /> </el-input
            ></el-col>
            <el-col :span="6">
              <div class="login_code" style="flex: 3">
                <img :src="codeUrl" /></div
            ></el-col>
            <el-col :span="4"
              ><p @click="getCode" class="get_code">换一张</p></el-col
            >
          </el-row>
        </el-form-item>
        <el-checkbox
          v-model="loginForm.rememberMe"
          style="margin: 0px 0px 25px 0px"
          >记住密码</el-checkbox
        >
        <el-form-item style="width: 100%">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width: 100%"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { decrypt, encrypt } from "@/utils/jsencrypt";
import { sysStyleGet } from "@/api/form/systemConfig";
import { changeStyle } from "@/utils";

export default {
  name: "Login",
  data() {
    const win = window.serverAPI;
    return {
      name: win.name,
      isUsername: false,
      isPassword: false,
      isValidCode: false,
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
        platform: "Manager",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" },
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" },
        ],
        code: [
          { required: true, trigger: "change", message: "验证码不能为空" },
        ],
      },
      loading: false,
      captchaOnOff: true,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.captchaOnOff =
          res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/png;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
        platform: "Manager",
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          // this.loginForm.password = encrypt(this.loginForm.password);
          let { username, password, rememberMe, code, uuid, platform } = {
            ...this.loginForm,
          };
          password = encrypt(password);

          this.$store
            .dispatch("Login", {
              username,
              password,
              rememberMe,
              code,
              uuid,
              platform,
            })
            .then(() => {
              sysStyleGet().then((res) => {
                console.log(res);
                const { code, data } = res;
                if (code === 200) {
                  const style = data.rows.find(
                    (item) => item.isDefault === "Y"
                  );
                  changeStyle(style.dictValue);
                  this.$router.push({ path: "/bigScreen" });
                } else {
                  this.msgError(res.msg);
                }
              });
            })
            .catch((err) => {
              console.log(err);
              this.msgError(err.msg);
              this.loading = false;
              this.getCookie();
              if (this.captchaOnOff) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>

<style rel="scss" lang="scss">
// 记住密码
.el-checkbox__inner {
  border-radius: 50%;
}
p {
  margin: 0;
}

.login {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  position: relative;
  background: #ffffff;
  .login_left {
    flex: 6;
    height: 100%;
    overflow: hidden;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .login_right {
    height: 100%;
    box-sizing: border-box;
    flex: 5;
    display: flex;
    align-items: center;
    justify-content: center;
    .login_form {
      width: 60%;
      .title {
        font-size: 2rem;
        font-weight: 700;
      }
    }
  }
}

.login_code {
  img {
    border-radius: 4px;
    width: 100%;
    height: 36px;
  }
}
.get_code {
  height: 100% !important;
  text-align: center;
  color: #3370ff;
  cursor: pointer;
}
</style>
